package com.location.test;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;

import com.R;
import com.worldwind.test.LocalDigitializeFeatureActivity;
import com.worldwind.test.RemoteDigitializeFeatureActivity;

import gov.nasa.worldwind.WorldWindow;
import gov.nasa.worldwind.globe.BasicElevationCoverage;
import gov.nasa.worldwind.layer.BackgroundLayer;
import gov.nasa.worldwind.layer.BlueMarbleLandsatLayer;
import gov.nasa.worldwind.layer.RenderableLayer;
import gov.nasa.worldwind.layer.TiandituCia_cLayer;
import gov.nasa.worldwind.layer.TiandituImg_cLayer;

/**
 * Created by Lenovo on 2018/4/28.
 */

public class LocationActivity  extends AppCompatActivity{

    private LocationService locationService =null;
    private WorldWindow wwd = null;
    private RenderableLayer locationLayer = null;
    private  RenderableLayer routeLayer = null;

    //controller view
    Button btnStartRoute = null;
    Button btnStopRoute = null;
    Button btnDeleteRoute = null;
    Button btnToggleRecord =null;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.location_layout);
        this.setTitle("轨迹记录");
        wwd = new WorldWindow(this);
        FrameLayout globeLayout = (FrameLayout) findViewById(R.id.globe);
        globeLayout.addView(this.wwd);
        //control route record
        btnStartRoute  = (Button)findViewById(R.id.btnStartRoute);
        btnStopRoute =(Button)findViewById(R.id.btnStopRoute);
        btnDeleteRoute=(Button)findViewById(R.id.btnDeleteRoute);
        btnToggleRecord=(Button)findViewById(R.id.toggleToRecord);
        //添加图层
        locationLayer = new RenderableLayer();
        routeLayer = new RenderableLayer();
        wwd.getLayers().addLayer(new BackgroundLayer());
        wwd.getLayers().addLayer(new BlueMarbleLandsatLayer());
        // Setup the World Window's elevation coverages.
        this.wwd.getGlobe().getElevationModel().addCoverage(new BasicElevationCoverage());
        //init map service layer
        wwd.getLayers().addLayer(new TiandituImg_cLayer());
        wwd.getLayers().addLayer(new TiandituCia_cLayer());
        //add location layer
        wwd.getLayers().addLayer(locationLayer);
        //add record route layer：添加记录轨迹的图层
        wwd.getLayers().addLayer(routeLayer);
        //定位服务
        locationService= new LocationService(this,wwd,locationLayer);
        locationService.startLocation();
    }

    @Override
    protected void onStart() {
        super.onStart();

        //轨迹记录
        btnStartRoute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationService.startRouteLocation(routeLayer);
            }
        });
        btnStopRoute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationService.stopRouteLocation();
            }
        });
        btnDeleteRoute.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                locationService.deleteRoute();
            }
        });
        btnToggleRecord.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), RemoteDigitializeFeatureActivity.class);
                startActivity(intent);
            }
        });

    }
}
